<?php

namespace app\service;

use Predis;
use think\facade\Config;
use think\facade\Log;

class RedisService
{

    static $client;

    static function initClient()
    {
        Log::info("> 开始：redis完成  ");
        self::$client = new Predis\Client([
            'scheme' => 'tcp',
            'host' => Config::get("redis.hostname"),
            'port' => 6379,
            "password" => Config::get("redis.password"),
        ]);
        Log::info("> 完成：初始化 redis完成  ");
    }

    function set(string $key, $value)
    {
        if(!empty($value)){
            return self::$client->set($key, json_encode($value));
        }
    }

    function keys($pattern)
    {
        return self::$client->keys($pattern);
    }

    function get(string $key)
    {
        return json_decode(self::$client->get($key) ?? '',true);
    }

}